<?php
namespace Admin\Controller;
use Think\Controller;
class OrdersController extends CommonController {

	public function _initialize(){
	parent::_initialize();
	$this->model="orders";
	$this->model_view="OrdersView";
	$this->key="order_id";	
	$this->title_index= L('index');
	$this->title_details=L('details');
	}
	
	//订单列表
	public function index(){
		$mist_arr=array("order_source_url"=>I("order_source_url"),"bill_firstname"=>I("bill_firstname"),"bill_lastname"=>I("bill_lastname"));
		$accute_arr=array('order_status'=>I("order_status"),"order_id"=>I("order_id"),
		"ips_id"=>I("ips_id"),"mh_oid"=>I("mh_oid"),"card_type"=>I("card_type"),
		"order_siteid"=>I("order_siteid"),"order_mid"=>I("order_mid"),"bill_email"=>I("bill_email"),
		"order_channel"=>I("order_channel"),"order_amount"=>I("order_amount"),"bill_phone"=>I("bill_phone"));
		$cookie_arr=array('order_status','order_mid','order_siteid');	
		$con=searchCon($mist_arr,$accute_arr,$cookie_arr);	
		
		cookie('order_status') && $cc_arr[]=array(L("order_status_name"),'order_status',order_status(cookie('order_status')));
		cookie('order_mid') && $cc_arr[]=array(L("mh_id"),'order_mid',cookie('order_mid'));
		cookie('order_siteid') && $cc_arr[]=array(L("site_id"),'order_siteid',cookie('order_siteid'));		
	    $this->assign("cookie_arr",$cc_arr);
		
		I("test_flag") && $con['ips_id']=array("neq","[TEST]");
		
		//时间区间
		if(I("order_time_start") && I("order_time_end") ){
			$con['order_time']=array("between",array(I("order_time_start"),I("order_time_end")));
		}
		//dump($con);
		
		if(I("gen_csv")){
			$sql=D($this->model_view)->fetchSql(true)->where($con)->order($this->key." desc")->select();
			parent::createCsv($sql,L("admin_orders_csv"),"admin-orders");
		}
			
		parent::index($con);
	}
	
	//订单历史查询
	public function orderhistory_qry(){
		$oop=M("orderhistory");
		$con['oh_oid']=I($this->key);
		$list=$oop->where($con)->order("oh_id desc")->select();
		//echo $oop->_sql();
		//dump($list);
		foreach($list as $key=>$value){
			$arr[$key]['oh_id']=$value['oh_id'];
			$arr[$key]['oh_oid']=$value['oh_oid'];
			$arr[$key]['oh_status']=order_status($value['oh_status']);
			$arr[$key]['oh_time']=$value['oh_time'];
			$arr[$key]['oh_amount']=$value['oh_amount'];
			$arr[$key]['oh_origin_amount']=$value['oh_origin_amount'];
			$arr[$key]['oh_gateway_amount']=$value['oh_gateway_amount'];
			$arr[$key]['oh_remark']=$value['oh_remark'];
		}
		//dump($arr);
		$this->ajaxReturn($arr);
	}
	
	//货运记录查询
	public function delivery_qry(){
		$oop=D("DeliveryView");
		$con['dv_oid']=I($this->key);
		$list=$oop->where($con)->order("dv_id desc")->select();
		$this->ajaxReturn($list);		
	}
	
	//处理交易的一些状态
	public function processOrder($oid,$order_status,$amount=0){
		
		$oop=new \Common\Common\OrderProcess($oid);
		switch($order_status){
			case 2://成功
			$result=$oop->toApprove();
			break;
			case 3://已授权
			$result=$oop->toAuthorize();
			break;
			case 4://已捕捉
			$result=$oop->toCapture();
			break;
			case 5://取消
			$result=$oop->toVoid();
			break;
			case 6://失败
			$result=$oop->toDecline();
			break;
			case 7://申请退款
			$reg="/^(\-|\+)?[\d]{1,10}\.[\d]{1,10}$/";
			if(!preg_match($reg,$amount)
			|| $amount<=0){
				$this->error(L("invalid_input"));
			}
			//dump($amount);exit;
			$result=$oop->toPendingRefund($amount);
			break;
			break;
			case 8://退款
			$result=$oop->toRefund();
			break;
			case 9://拒付
			//echo $cb_amount;exit;
			//拒付需要知道金额,如果小于0
			$reg="/^(\-|\+)?[\d]{1,10}\.[\d]{1,10}$/";
			if(!preg_match($reg,$amount)
			|| $amount<0){
				$this->error(L("invalid_input"));
			}
			$result=$oop->toChargeback($amount);
			break;
		}
		if($result)
			$this->success(L("action_success"));
		else
			$this->error(L("action_failed"));
	}

	/**
	*添加黑名单
	*@param int $oid 订单号
	*@param string $type 黑名单类型
	*/
	public function inputBlacklist($oid,$type){
		$con['order_id']=$oid;
		$list=M("orders")->where($con)->find();
		$list || $this->error(L("unknow_order"));
		
		switch($type){
			case "CreditCard":
			$data['bl_value']=$list['cc_cardid'];
			$data['bl_value1']=$list['cc_cvv'];
			$data['bl_value2']=$list['cc_expiry_month'];
			$data['bl_value3']=$list['cc_expiry_year'];		
			break;
			case "Phone":
			$data['bl_value']=$list['bill_phone'];
			break;
			case "Email":
			$data['bl_value']=$list['bill_email'];
			break;
			case "IP":
			$data['bl_value']=$list['order_source_ip'];
			break;
			case "Country":
			$data['bl_value']=$list['bill_country'];
			break;
		}
		$data['bl_remark']="Order ID:".$oid;
		$result=parent::inputBlacklist($data,$type);
		if($result)
			$this->success(L("add_success"));
		else
			$this->error(L("add_failed"));
	}
	
	/**
	*下载CSV文件
	*/
	public function downloadCsv(){
		parent::downloadCsv("admin-orders.csv");
	}
	
	/**
	*标记为风险订单
	*/
	public function flagRisk($oid,$desc){
		$oop=M($this->model);
		$con['order_id']=$oid;
		$data['order_isrisk']=1;
		$data['order_risk_desc']=$desc;
		if($oop->where($con)->save($data))
			$this->success(L("action_success"));
		else
			$this->error(L("action_failed"));		
	}
	
	/**
	*取消风险订单标记
	*/
	public function cancelRisk($oid){
		$oop=M($this->model);
		$con['order_id']=$oid;
		$data['order_isrisk']=0;
		$data['order_risk_desc']="";
		if($oop->where($con)->save($data))
			$this->success(L("action_success"));
		else
			$this->error(L("action_failed"));
	}
	
}